package com.yanqu.road.dao.impl.mogulbirthday;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.mogulbirthday.MogulBirthdayUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class MogulBirthdayUserDataDaoImpl extends TempDataObjectDao<MogulBirthdayUserData> {


    public static String getMogulBirthdayUserDataSql = "select * from `t_u_mogul_birthday` where `userId`=?";
    public MogulBirthdayUserData getMogulBirthdayUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMogulBirthdayUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateMogulBirthdayUserDataSql = "update `t_u_mogul_birthday` set getReward=?,resetTime=?,notify=?,readPostList=?,dayResetTime=?,birthday=? where userId=?";
    @Override
    protected boolean updateData(MogulBirthdayUserData mogulBirthdayUserData) {
        mogulBirthdayUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, mogulBirthdayUserData.isGetReward()));
        para.put(i++, new DbParameter(Types.INTEGER, mogulBirthdayUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, mogulBirthdayUserData.isNotify()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(mogulBirthdayUserData.getReadPostList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, mogulBirthdayUserData.getDayResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayUserData.getBirthday()));
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayUserData.getUserId()));
        if (getDb().executeNoneQuery(updateMogulBirthdayUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMogulBirthdayUserDataSql = "insert into `t_u_mogul_birthday`(userId,getReward,resetTime,notify,readPostList,dayResetTime,birthday) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MogulBirthdayUserData mogulBirthdayUserData) {
        mogulBirthdayUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, mogulBirthdayUserData.isGetReward()));
        para.put(i++, new DbParameter(Types.INTEGER, mogulBirthdayUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, mogulBirthdayUserData.isNotify()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(mogulBirthdayUserData.getReadPostList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, mogulBirthdayUserData.getDayResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, mogulBirthdayUserData.getBirthday()));
        if (getDb().executeNoneQuery(insertMogulBirthdayUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MogulBirthdayUserData parseData(ResultSet rs) throws SQLException {
        MogulBirthdayUserData data = new MogulBirthdayUserData();
        data.setUserId(rs.getLong("userId"));
        data.setGetReward(rs.getBoolean("getReward"));
        data.setResetTime(rs.getInt("resetTime"));
        data.setNotify(rs.getBoolean("notify"));
        data.setReadPostList(StringUtils.stringToLongList(rs.getString("readPostList"), ","));
        data.setDayResetTime(rs.getInt("dayResetTime"));
        data.setBirthday(rs.getLong("birthday"));

        return data;
    }
}
